#include <iostream>
#include <iomanip>
#include <fstream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;

namespace PLAYVIDEO
{

	#define VIDEO "D:\\workshop\\video\\Megamind.avi"
	int playVideo()
	{
		Mat frameImage ;
		int frameNum = -1;   
		char *WinName = "my video" ;
		VideoCapture myVideo(VIDEO);
		if( !myVideo.isOpened())
		{
			cout<<"open video failed!" <<endl;
			return -1;
		}      
		Size videoSize = Size(myVideo .get(CV_CAP_PROP_FRAME_WIDTH), myVideo.get (CV_CAP_PROP_FRAME_HEIGHT));
		namedWindow(WinName,CV_WINDOW_AUTOSIZE );
		cvMoveWindow(WinName,400,100);
		int allFrameNumber = myVideo .get(CV_CAP_PROP_FRAME_COUNT);
		cout<<"all=" <<allFrameNumber<< endl;
		bool cyclePlay = false ;
		while(1)
		{
			++frameNum;
			myVideo >> frameImage;
			if (cyclePlay )
			{
				if ( frameNum == allFrameNumber-1  )
				{
					myVideo.set(CV_CAP_PROP_POS_FRAMES,0);
					frameNum=-1;
				}
			}
			else if ( frameImage.empty())
			{      
				break;
			}
			cout<<frameNum<< endl;
			string outImageName;
			if(frameNum<20) 
			{
				outImageName = format("outimage%d.png" , frameNum-1);
				imwrite(outImageName,frameImage);
			}
			imshow(WinName,frameImage );
			int c = waitKey(1);
			if (c == 27) break;
		}
		return 0;
	}

	int smallImage()
	{
		Mat img1 = imread("D:\\workshop\\vs2010\\BeliefPropagation\\BrownUniversity\\bp-vision\\bpStereo\\tsukuba1.pgm");
		Mat img2 = imread("D:\\workshop\\vs2010\\BeliefPropagation\\BrownUniversity\\bp-vision\\bpStereo\\tsukuba2.pgm");
		Mat img_o1 = img1.rowRange(10,80);
		Mat img_o2 = img2.rowRange(10,80);
		imshow( "img_o1",img_o1 );
		imshow( "img_o2",img_o1 );
		imwrite("D:\\workshop\\vs2010\\BeliefPropagation\\BrownUniversity\\bp-vision\\bpStereo\\tsukuba_o1.pgm",img_o1);
		imwrite("D:\\workshop\\vs2010\\BeliefPropagation\\BrownUniversity\\bp-vision\\bpStereo\\tsukuba_o2.pgm",img_o2);
		waitKey(0);
		return 0;
	}
}